public with sharing class WBSfdcInfo {
	
	public class UserWeiboInfo{
		public string code{get;set;}
		public string access_token{get;set;}
		public decimal expires_in{get;set;}
		public string uid{get;set;}
	}
	
	//get WBSfdcInfo instance. 
	private static WBSfdcInfo info;
	public static WBSfdcInfo instance(){
		if(info == null){
			info = new WBSfdcInfo();
		}
		return info;
	}
	//
	// ---------------- property ------------------
	//weibo code
	public string code{get{
		return weiboInfo.code;
	}
	set{
		weiboInfo.code = code;
	}}
	// access token
	public string token{get{
		return weiboInfo.access_token;
	}
	set{
		weiboInfo.access_token = token;
	}}
	//current weibo user information.
	public UserWeiboInfo weiboInfo{
		get{
			if(weiboInfo == null){
				weiboInfo = new UserWeiboInfo();
		    User u = [select weibo_code__c,weibo_access_token__c,weibo_expires_in__c,weibo_uid__c
		    					from user where id=:UserInfo.getUserId()];
		    weiboInfo.code = u.weibo_code__c;
		    weiboInfo.access_token = u.weibo_access_token__c;
		    weiboInfo.expires_in = u.weibo_expires_in__c;
		    weiboInfo.uid  = u.weibo_uid__c;
			}
			return weiboInfo;
		}
		set{
		    User u = [select weibo_code__c,weibo_access_token__c,weibo_expires_in__c,weibo_uid__c
		    					from user where id=:UserInfo.getUserId()];
		    u.weibo_code__c = weiboInfo.code;
		    u.weibo_access_token__c = weiboInfo.access_token;
		    u.weibo_expires_in__c = weiboInfo.expires_in;
		    u.weibo_uid__c = weiboInfo.uid;
		    update u;
		}
	}

	// ----------------set Weibo Code ------------------
	public static void WeiboCode(string code) {
    try{
			WBSfdcInfo.UserWeiboInfo weiboinfo = WBSfdcInfo.instance().weiboInfo;
      weiboinfo.code = code;
      requestToken();
    }
    catch(Exception e){
      System.debug(e);
    }
	}

	public static string requestToken() {
		if(WBUtil.isNvl(WBSfdcInfo.instance().token)){
			try{
				WB_OpenApi.WB_oauth2_access_token_in wb_in = new WB_OpenApi.WB_oauth2_access_token_in(); 
				string jsonResult = WBUtil.sendRequest(wb_in.toRequestUrl());
				if(jsonResult.indexOf('error',0) == -1){
					// get response content
					WB_OpenApi.WB_oauth2_access_token_out wb_out = new WB_OpenApi.WB_oauth2_access_token_out();
					wb_out.parse(jsonResult); 
					//set current user weibo information.
					WBSfdcInfo.UserWeiboInfo weiboinfo = WBSfdcInfo.instance().weiboInfo;
					weiboInfo.access_token = wb_out.access_token;
					weiboInfo.expires_in = wb_out.expires_in;  
					weiboInfo.uid = wb_out.uid;
					weiboInfo.code = wb_in.code; 
					WBSfdcInfo.instance().weiboInfo = weiboInfo;
				}
			}
			catch(Exception e){
				 ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, e.getMessage()));
			}
		}
		return WBSfdcInfo.instance().token;
	}
	
	// ----------------reset Weibo user information ------------------
	public void resetWeiboUserInfo() {
	    User u = [select id,weibo_code__c,weibo_access_token__c,weibo_expires_in__c,weibo_uid__c
	    					from user where id=:UserInfo.getUserId()];
	    u.weibo_code__c = '';
	    u.weibo_access_token__c =  '';
	    u.weibo_expires_in__c = 0;
	    u.weibo_uid__c = '';
	    update u;
	}
	
	// ----------------Set Lead Uid, Account, Contact UID ------------------
	public static void LeadUid(string lead_id,string weibo_uid){
		try{
			if(!WBUtil.isNvl(lead_id) && !WBUtil.isNvl(weibo_uid)){
				WBSfdcInfo.instance().setLeadUID(lead_id, weibo_uid);
			}
		}
		catch(Exception e){
			 ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, e.getMessage()));
		}
	}
	// ----------------Lead, Account, Contact UID ------------------
	// set specified lead weibo uid.
	public void setLeadUID(ID pId,string weiboUID){
		Lead l = [select id,weibo_uid__c from lead where id=:pId];
		l.weibo_uid__c = weiboUID;
		update l;
	}
	// get specified lead weibo uid.
	public string getLeadUID(ID pId){
		return [select weibo_uid__c from lead where id=:pId].weibo_uid__c;
	}
	
	// check specified lead weibo id is exist or not.
	public boolean LeadHasUID(ID pId){
		string uid = getLeadUID(pId);
		return (uid != null && uid != '');
	}
	
	//TODO
	// set specified account weibo uid.
	//TODO
	// set specified contact weibo uid.
	//TODO
	// get specified account weibo id.
	//TODO
	// get specified contact weibo id.
	//TODO
	// check specified account weibo id is exist or not.
	//TODO
	// check specified contact weibo id is exist or not.
}